/*
	https://openapi.esign.cn 正式生产环境地址
	https://smlopenapi.esign.cn  沙箱环境
	e签宝对接接口
	X-Tsign-Open-App-Id  应用ID，通过e签宝开放平台获取。
	X-Tsign-Open-Token  通过获取鉴权Token接口返回
	Content-Type   application/json; charset=UTF-8
*/
// const baseUrl = 'https://openapi.esign.cn'

const baseUrl = 'https://smlopenapi.esign.cn'
const commonHeaders = {
	'Content-Type': 'application/json; charset=UTF-8',
	'X-Tsign-Open-App-Id': '7438978534',
	'X-Tsign-Open-Auth-Mode': 'Signature',
	'X-Tsign-Open-Ca-Timestamp': new Date().getTime(),
	// 'X-Tsign-Open-Ca-Signature': 'HTTPMethod:POST\nAccept:*/*\n',
	'Accept': '/',
}
// /v3/doc-templates/{docTemplateId}
const temDetailGet = (docTemplateId) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseUrl}/v3/doc-templates/${docTemplateId}`,
			method: 'GET',
			header: commonHeaders,
			success: (res) => {
				resolve(res)
			},
			fail: (err) => {
				uni.showToast({
					title: '未获取到合同详情',
					mask: true
				})
				reject(err)
			}
		})
	})
}
// 通过页面发起签署
const modelDetailGet = docTemplateId => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: `${baseUrl}/v3/sign-flow/sign-flow-initiate-url/by-file`,
			method: 'POST',
			header: {
				'content-type': 'application/json; charset=UTF-8',
				'X-Tsign-Open-App-Id': '7438978534',
				'X-Tsign-Open-Auth-Mode': 'Signature',
				'X-Tsign-Open-Ca-Timestamp': new Date().getTime(),
				'Accept': '/',
			},
			data: {
				"initiatePageConfig": {
					"customBizNum": "custom",
					"redirectUrl": "https://www.baidu.cn",
					"uneditableFields": [
						"attachments"
					]
				},
				"signFlowConfig": {
					"signFlowTitle": "租赁服务协议",
					"autoFinish": true,
					"noticeConfig": {
						"noticeTypes": "1"
					},
					"redirectConfig": {
						"redirectUrl": "https://xxx.cn/"
					},
					"notifyUrl": "http://xx.xx.xx.xx:8081/notify"
				}
			},
			success: (res) => {
				resolve(res)
			},
			fail: (err) => {
				uni.showToast({
					title: '未获取到合同详情',
					mask: true
				})
				reject(err)
			}
		})
	})
}

export default {
	modelDetailGet,
	temDetailGet,
}